package com.android.projectsquirrel;

import com.android.projectsquirrel.History.CustomLocationListener;

import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;

import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class Main extends ListActivity {
    /** Called when the activity is first created. */
    String selectedActivity;
    String[] menuItems;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        final String selectedActivity;
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTitle(getText(R.string.title_main));
        ListView lv = getListView();
  	  	lv.setTextFilterEnabled(true);     
  	  	Resources res = getResources();
  	  	
  	  	/*Location information stuff after this line */
		LocationManager locationManager = 
			(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		LocationListener locationListener = new CustomLocationListener();
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);
		/* End location initiation */
		
		//Populate the menu based on the string array
  	  	menuItems = res.getStringArray(R.array.main_menu_items);

  	  	setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, menuItems));
	  	
  	  	lv.setOnItemClickListener(new OnItemClickListener() {
  	    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  	    	String selectedActivity = ((TextView) view).getText().toString();

  	    	
  	    	//New sighting
  	    	if (selectedActivity.equals(menuItems[0]) ) {
  	  	    	startActivity(new Intent(Main.this, SurveyPart1.class));
  	    	}
  	    	//Share a Photo
  	    	else if (selectedActivity.equals(menuItems[1])) {
  	  	    	startActivity(new Intent(Main.this, camera.class));
  	    	}
  	    	//History
  	    else if (selectedActivity.equals(menuItems[2])) {
  	  	    	startActivity(new Intent(Main.this, MapListing.class));
  	  	   
  	    	}
  	    	//What people have said
  	    	else if (selectedActivity.equals(menuItems[3])) {
  	  	    	startActivity(new Intent(Main.this, com.android.projectsquirrel.WhatPeopleHaveSaid.class));
  	    	}
  	    	//Gray vs. Fox Squirrels
  	    	//else if (selectedActivity.equals(menuItems[4])) {
  	  	    	//startActivity(new Intent(Main.this, FoxGrey.class));
  	    	//}
  	    	//What we have learned
  	    	else if (selectedActivity.equals(menuItems[4])) {
  	  	    	startActivity(new Intent(Main.this, whatwehavelearned.class));
  	    	}
  	    	//Profile List
  	    	else if (selectedActivity.equals(menuItems[5])) {
  	  	    	startActivity(new Intent(Main.this, ProfileListing.class));
  	    	}
  	    //FAQ
  	    	else if (selectedActivity.equals(menuItems[6])) {
  	  	    	startActivity(new Intent(Main.this, FAQandContact.class));
  	    	}
  	    }
  	  });
  	    


  	  
  	}
	
	public class CustomLocationListener implements LocationListener {
		
		@Override
		public void onLocationChanged(Location location) {
			//Update the String class
			String lat = "Latitude " + location.getLatitude() + "\n";
			lat = lat + "Longitude " + location.getLongitude();
			Toast.makeText(getApplicationContext(), lat, 
					Toast.LENGTH_SHORT).show();
			GPSHelper.latitude = location.getLatitude();
			GPSHelper.longitude = location.getLongitude();
			
		}
		
		@Override
		public void onProviderDisabled(String provider ){
			Toast.makeText(getApplicationContext(), "GPS Disabled", 
					Toast.LENGTH_SHORT).show();
		}
		@Override
		public void onProviderEnabled(String provider) {
			Toast.makeText(getApplicationContext(), "GPS Enabled", 
					Toast.LENGTH_SHORT).show();
		}
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			//Do nothing
		}
	}


  }